#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QPluginLoader>
#include <QDebug>

void testDatabaseOperations() {
    // 使用绝对路径加载驱动
    QString driverPath = "C:/xdev/Qt/6.9.2/Src/qtbase/src/plugins/sqldrivers/duckdb/build_qduckdb/plugins/sqldrivers/qsqlduckdb.dll";
    if (!QSqlDatabase::isDriverAvailable("QDUCKDB")) {
        QPluginLoader loader(driverPath);
        if (!loader.load()) {
            qDebug() << "Failed to load driver:" << loader.errorString();
            return;
        }
    }
    
    QSqlDatabase db = QSqlDatabase::addDatabase("QDUCKDB");
    db.setDatabaseName(":memory:"); // 使用内存数据库
    
    if (!db.open()) {
        qDebug() << "Failed to open database:" << db.lastError().text();
        return;
    }

    // 1. 创建表
    QSqlQuery query;
    if (!query.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name VARCHAR(50), value DOUBLE)")) {
        qDebug() << "Create table failed:" << query.lastError().text();
        return;
    }
    qDebug() << "Table created successfully";

    // 2. 插入数据
    if (!query.exec("INSERT INTO test VALUES (1, 'Item1', 10.5)")) {
        qDebug() << "Insert failed:" << query.lastError().text();
        return;
    }
    if (!query.exec("INSERT INTO test VALUES (2, 'Item2', 20.3)")) {
        qDebug() << "Insert failed:" << query.lastError().text();
        return;
    }
    qDebug() << "Data inserted successfully";

    // 3. 查询数据
    if (!query.exec("SELECT * FROM test")) {
        qDebug() << "Select failed:" << query.lastError().text();
        return;
    }
    
    qDebug() << "Query results:";
    while (query.next()) {
        qDebug() << "ID:" << query.value(0).toInt()
                << "Name:" << query.value(1).toString()
                << "Value:" << query.value(2).toDouble();
    }

    // 4. 更新数据
    if (!query.exec("UPDATE test SET value = 15.0 WHERE id = 1")) {
        qDebug() << "Update failed:" << query.lastError().text();
        return;
    }
    qDebug() << "Data updated successfully";

    // 5. 删除数据
    if (!query.exec("DELETE FROM test WHERE id = 2")) {
        qDebug() << "Delete failed:" << query.lastError().text();
        return;
    }
    qDebug() << "Data deleted successfully";

    // 6. 再次查询验证
    if (!query.exec("SELECT * FROM test")) {
        qDebug() << "Select failed:" << query.lastError().text();
        return;
    }
    
    qDebug() << "Final query results:";
    while (query.next()) {
        qDebug() << "ID:" << query.value(0).toInt()
                << "Name:" << query.value(1).toString()
                << "Value:" << query.value(2).toDouble();
    }

    db.close();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    qDebug() << "Available SQL drivers:" << QSqlDatabase::drivers();
    qDebug() << "QDUCKDB available:" << QSqlDatabase::isDriverAvailable("QDUCKDB");
    
    testDatabaseOperations();
    
    return 0;
}